package com.keqing.tomato;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.keqing.tomato.fragment.FragmentHistory;
import com.keqing.tomato.fragment.FragmentSettings;
import com.keqing.tomato.fragment.FragmentTomatoTime;

public class MainActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener {
    private FragmentManager manager = getSupportFragmentManager();
    private Fragment fragmentHistory;
    private FragmentSettings fragmentSettings;
    private FragmentTomatoTime fragmentTomatoTime;
    private TextView titleTv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.e("com/keqing/tomato","MainActivity - onCreate()");

        setContentView(R.layout.activity_main);
        titleTv = findViewById(R.id.title);

        fragmentHistory = new FragmentHistory();
        fragmentSettings = new FragmentSettings();
        fragmentTomatoTime = new FragmentTomatoTime();
        initFragPage();
        BottomNavigationView bottomNavigationView = findViewById(R.id.bottomNav);
        bottomNavigationView.setOnNavigationItemSelectedListener(this);
    }

    private void initFragPage() {
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.add(R.id.fragmentContainer, fragmentHistory);
        transaction.add(R.id.fragmentContainer, fragmentSettings);
        transaction.add(R.id.fragmentContainer, fragmentTomatoTime);

        transaction.hide(fragmentHistory);
        transaction.hide(fragmentSettings);
        transaction.commit();
        titleTv.setText(R.string.menu_item_tomato);
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        FragmentTransaction transaction = manager.beginTransaction();
        switch (item.getItemId()) {
            case R.id.tomato_time:
                transaction.hide(fragmentHistory);
                transaction.hide(fragmentSettings);
                transaction.show(fragmentTomatoTime);
                titleTv.setText(R.string.menu_item_tomato);
                break;
            case R.id.history:
                transaction.show(fragmentHistory);
                transaction.hide(fragmentSettings);
                transaction.hide(fragmentTomatoTime);
                titleTv.setText(R.string.menu_item_history);
                break;
            case R.id.mySettings:
                transaction.hide(fragmentHistory);
                transaction.show(fragmentSettings);
                transaction.hide(fragmentTomatoTime);
                titleTv.setText(R.string.menu_item_setting);
                break;
        }
        transaction.commit();
        return true;
    }

    @Override
    public void onBackPressed() {
        //super.onBackPressed();
        Log.e("com/keqing/tomato","MainActivity - onBackPressed()");
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addCategory(Intent.CATEGORY_HOME);
        startActivity(intent);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.e("com/keqing/tomato","MainActivity - onDestroy()");
    }
}
